﻿package com.wly.ui.gamescene
{
	import com.wly.box2d.BoxItem;
	import com.wly.control.gamescene.HitItemControl;
	import com.wly.ui.common.BaseSprite;
	import com.wly.ui.utils.CommonUtils;
	
	import flash.display.DisplayObject;
	
	import Box2D.Dynamics.b2Body;
	
	public class HitItemGrid extends BaseSprite
	{
		private var _items:Array;
		private var _boxItemArr:Array;
		
		public function HitItemGrid()
		{
			super();
			init();	
		}
		
		private function init():void
		{
			_boxItemArr = [];
			var boxItem:BoxItem;
			var item:HitItem;
			_items = [];
			_boxItemArr = [];
			var i:int;
			for(i=0; i<10; i++)
			{
				item = getChildByName("item"+i.toString()) as HitItem;
				if(item)
				{
					boxItem = new BoxItem();
					boxItem.initWithSprite(item, true);
					boxItem.bX = item.x+x;
					boxItem.bY = item.y+y;
					boxItem.b2Type = b2Body.b2_staticBody;
					_boxItemArr.push(boxItem);
					item.parentGrid = this;
					_items.push(item);
				}
			}
		}
		
		public function getBoxItemArr():Array
		{
			return _boxItemArr;
		}
		
		override protected function onControlDeattach():void
		{
			deattachItemCtrls();
		}
		
		public function updateItemCtrls(ctrls:Array):void
		{
			if(!ctrls || !_items)
			{
				return;
			}
			
			var len:int = ctrls.length>_items.length?_items.length:ctrls.length;
			var i:int;
			var item:HitItem;
			var itemCtrl:HitItemControl;
			deattachItemCtrls();
			
			for(i=0; i<len; i++)
			{
				item     = _items[i] as HitItem;
				itemCtrl = ctrls[i] as HitItemControl;
				if(item && itemCtrl)
				{
					item.deattachUIControl();
				}
			}
		}
		
		private function deattachItemCtrls():void
		{
			if(!_items)
			{
				return;
			}
			
			var i:int;
			var item:HitItem;
			
			for(i=0; i<_items.length; i++)
			{
				item = _items[i] as HitItem;
				if(item)
				{
					item.deattachUIControl();
				}
			}
		}
	}
}